home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fritz: All Fritz
/
All Fritz.zip
/
All Fritz
/
FILES
/
PROGNG_C
/
CSUBR.LZH
/
FEXPND.C
< prev
next >
Wrap
C/C++ Source or Header
|
1985-11-17
|
8KB
|
310 lines
/*******************************************************/
/*
fexpnd returns a pointer to the next filename.ext which
matches the first parameter (str) or a zero is no file
matches that parameter. The given parameter is processed
to generate a normalized search string and a normalized
path prefix. The prefix is returned in the area pointed to
by the second parameter. This string may be concatenated
with the returned value to give a fully-qualified dataset
name for open, rename, erase, etc.
You should continue calling fexpnd with the same first
parameter until it returns a zero, then pass it a new first
parameter. This can be used to expand a list of ambiguous
filenames on a parameter list. */
char mydta[80] = {0};
char fxs[80] = {0};
char fxsrch[80] = {0};
char fxpref[80] = {0};
char *fexpnd(str, path)
char *str, *path;
{
int getret, fixpath(), strcpy(), dirnxt(), dirfst();
int savdta(), restdta();
char *rindex();
savdta(); /* save old dta in an unknown location */
setdta(&mydta); /* make MS-DOS use my dta */
if (strcmp(str,fxs) == 0)
getret = dirnxt(); /* MS-DOS 2.0+ get next function */
else {
strcpy(fxs,str); /* for detection of a new name */
if (!fixpath(str, fxsrch, fxpref))
return (0);
strcpy(path, fxpref); /* give path to caller */
getret = dirfst(fxsrch); /* MS-DOS 2.0+ get first function */
}
restdta(); /* restore old dta */
if (getret == 0) {
return(&mydta[30]); /* return filename.ext */
}
else
return 0;
}
int dtads, dtadx;
savdta()
{
#asm
PUSH AX
PUSH ES
PUSH BX
MOV AH,2fh
INT 21h
MOV WORD dtads_,ES
MOV WORD dtadx_,BX
POP BX
POP ES
POP AX
#
}
restdta()
{
#asm
PUSH DS
PUSH DX
PUSH AX
MOV DX,WORD dtads_
MOV DS,DX
MOV DX,WORD dtadx_
MOV AH,1AH
INT 21h
POP AX
POP DX
POP DS
#
}
char *dtap;
setdta(newdta)
char *newdta;
{
dtap = newdta;
#asm
PUSH DX
PUSH AX
MOV DX,WORD dtap_
MOV AH,1AH
INT 21h
POP AX
POP DX
#
}
char *dirfnp;
int dirret;
dirfst(s)
char *s;
{
dirfnp = s;
#asm
PUSH DX
PUSH CX
MOV DX,WORD dirfnp_
MOV AH,4eh
INT 21h
JC D56X1
MOV AX,0
D56X1: NOP
MOV WORD dirret_,AX
POP CX
POP DX
#
return dirret;
}
dirnxt()
{
#asm
MOV AH,4fh
INT 21h
JC D56X2
MOV AX,0
D56X2: NOP
MOV WORD dirret_,AX
#
return dirret;
}
/* ================================================================ */
/*
From Dr. Dobbs #108 October 1985. */
/* fixpath() processes a DOS pathname for two different uses.
The input path, *ip, is presumably a command operand like the
first operand of DIR. One output, the search path *sp, is
the input possibly augmented with "*.*" or "\*.*" so that it
will work reliably with DOS functions 4E/$f. The other output
is a lead-in path, *lip, which can be prefixed to the simple
filename.ext returned by fuctions 4E/4F to make a complete path
for opening or erasing a file.
The function returns 1 if it is successful, but 0 if the
input path can't be processed and should not be used.
Some input paths can be processed here yet be invalid or
useless. The search path produced from such an input wwill
cause an error return from function 4E (search first match). */
/* ================================================================ */
int fixpath(ip, sp, lip)
register char *ip, /* input path */
*sp, /* the search path */
*lip; /* the lead-in or prefix path */
{
char *cp; /* work pointer for scanning paths */
int attr; /* attribute for chgattr */
int ret, strlen(), strcpy(), strcmp(), strcat(), chgattr();
/* ================================================================ */
/* Pick off 4 special cases:
(1) the NULL string, which we take to mean "*.*"
(2) the simple "d:" reference to a drive, which we also take
to mean "d:*.*"
(3) the root-dir reference "d:\" which is mishandled by
function 0x4300 of both DOS 2.1 and 3.0.
(4) any reference that ends in "\"
In all cases, the input path is ok as a lead-in, but it needs
the global qualifier *.* added for searching.
/* ================================================================ */
if ((strlen(ip) == 0) /* null string */
|| (strcmp(ip+1, ":") == 0) /* d: only */
|| (ip[strlen(ip)-1] == '\\')) /* ends in backslash */
{
strcpy(lip, ip); /* input is ok as prefix */
strcpy(sp, ip);
strcat(sp, "*.*"); /* add *.* for search */
return (1);
}
/* ================================================================ */
/* Ok, we have a non-null string not ending in \ and not a lone
drive letter. Four possibilities remain:
(1) an explicit file reference, b:\mydir\mydat
(2) an explicit directory reference, \mydir
(3) an ambiguous file reference, *.* or b:\mydir\*.dat
(4) an unknown name, a:noway or b:\mydir\nonesuch.dat
We can separate this with fair reliability using DOS function
0x4300, get attributes from path.
/* ================================================================ */
attr = 0x0000; /* output area for get command */
ret = chgattr('G', ip, &attr); /* get attributes for this path */
if (ret == 0x0002)
/* the path is valid, in that all directories and drives
named in it are valid, but the final name is unknown. No
files will be found in a search, so quit now. */
return (0);
if ((ret == 0x0003)
|| ((ret == 0) && ((attr & 0x0010) == 0))) {
/* Error 3, path not found, could mean total junk or a
misspelt directory name, but most likely it just means
the path ends in an ambiguous name. If there's an error
the initial search (function 4Eh) will fail.
With no error and reg cx not showing the directory
attribute flags 0010, we have a normal, unambiguous file
pathname like "b:\mydir\mydat" or just "myprog.com".
In either case the search path is the whole input
path. The lead-in is that shorn of whatever follows the
rightmost \ or :, whichever comes last -- or just a null
string if there is no \ or :. */
strcpy(sp, ip); /* working copy of ip in sp */
cp = sp + strlen(sp) -1;
for (; cp > sp; --cp)
if (('\\' == *cp) || (':' == *cp))
break;
if (cp > sp)
++cp; /* retain colon or slash */
*cp = '\0'; /* make a null string */
strcpy(lip, sp);
strcpy(sp, ip); /* whole input for search */
return (1);
}
if ((ret == 0) && (attr & 0x0010)) {
/* No error and the directory attribute returned in cx
shows an unambiguous path that happpens to end in the
name of a directory, for instance "..\..\bin" or
"b:\mydir". Applying the rules of DIR or COPY, we
have to treat these as global refs to all files named in
the directory. The search path is the input with
"\*.*" tacked onto it. The lead-in path is just the
input plus a backslash. */
strcpy(sp, ip);
strcpy(lip, ip);
strcat(sp, "\\*.*");
strcat(lip, "\\");
return (1);
}
else {
/* unexpected events make me nervous, so give up */
return (0);
}
}
int rax, rcx;
char *rdx;
int chgattr(getset, path, attr)
char getset, *path;
int *attr;
{
if (getset == 'S') {
rcx = *attr;
rax = 0x4301;
}
else
rax = 0x4300;
rdx = path;
#asm
PUSH DX
PUSH CX
PUSH AX
MOV DX,WORD rdx_
MOV AX,WORD rax_
MOV CX,WORD rcx_
INT 21H
MOV WORD rax_,AX
JC D56X9
MOV WORD rax_,0
D56X9: NOP
MOV WORD rcx_,CX
POP AX
POP CX
POP DX
#
if (rax == 0)
*attr = rcx;
return(rax);
}